package com.spider.site.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import com.spider.site.entity.User;
import com.spider.site.service.SpiderService;

/**
 * 入门的 controller
 * @author wq
 */
@Controller
public class MainController {

	@Autowired
	private SpiderService spiderService;
	
	@GetMapping("/{url}")
	public String index(@PathVariable String url) {
		if(url != null && !url.equals("index") && !url.contains("/spider")) {
			return "404";
		}
		return "index";
	}
	
	@GetMapping("/")
	public String main() {
		return "index";
	}
	
	@GetMapping("/manger")
	public String manager() {
		return "mangerControl";
	}
	
	@GetMapping("/result")
	public String toResult() {
		return "dataShow";
	}
	
	@GetMapping("/spiderToLogin")
	public String toLogin() {
		return "login";
	}
	
	/**
	 * 登录 
	 * @param user
	 * @param url 登录成功后返回
	 * @return
	 */
	@PostMapping("/spiderLogin")
	public String spiderLogin(HttpServletRequest request , User user) {
		System.out.println("user : " + user.getLoginId()  + " -- " +  user.getPassword());
		if(user != null) {
			if(this.spiderService.isLogin(user)) {
				request.getSession().setAttribute("spiderUser", user);
				return "mangerControl";
			}
		}
		return "login";
	}
	
	
}
